﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Siemens.Engineering;
using Siemens.Engineering.HW;
using Siemens.Engineering.HW.Features;
using Siemens.Engineering.SW;
using Siemens.Engineering.SW.Blocks;
using Siemens.Engineering.SW.ExternalSources;
using Siemens.Engineering.SW.Tags;
using Siemens.Engineering.SW.Types;
using Siemens.Engineering.Hmi;
using HmiTarget = Siemens.Engineering.Hmi.HmiTarget;
using Siemens.Engineering.Hmi.Tag;
using Siemens.Engineering.Hmi.Screen;
using Siemens.Engineering.Hmi.Cycle;
using Siemens.Engineering.Hmi.Communication;
using Siemens.Engineering.Hmi.Globalization;
using Siemens.Engineering.Hmi.TextGraphicList;
using Siemens.Engineering.Hmi.RuntimeScripting;
using Siemens.Engineering.Compiler;
using Siemens.Engineering.Library;
using System.IO;
namespace TIA_Openness
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting TIA Portal");
            using (TiaPortal tiaPortal = new TiaPortal(TiaPortalMode.WithoutUserInterface)) //打开博途
            {
                Console.WriteLine("TIA Portal has started");
                ProjectComposition projects = tiaPortal.Projects;
                Console.WriteLine("输入项目文件路径");
                // please adapt the path and the extension apx to the installed version of TIA Portal
                //项目文件路径
                string fileString = Console.ReadLine();

                FileInfo projectPath = new FileInfo(fileString);
                
                Project _project = null;

                //打开项目
                try
                {
                    _project = projects.OpenWithUpgrade(projectPath);
                }
                catch (Exception)
                {
                    Console.WriteLine(String.Format("Could not open project {0}", projectPath.FullName));
                    Console.WriteLine("Demo complete hit enter to exit");
                    Console.ReadLine();
                }
                //打开完成
                Console.WriteLine(String.Format("Project {0} is open", _project.Path.FullName));

                //新建PLC
                //DeviceComposition devices = _project.Devices;
                //Device newDevice = devices.CreateWithItem(@"OrderNumber:6ES7 511-1AK02-0AB0/V2.6", "PLC_11", "NewDevice");

                //枚举设备
                GetDevices.EnumerateDevicesInGroups(_project);

                for (int i = 1; i <= GetDevices.list.Count; i++)
                {
                    string deviceName = null;
                    foreach (var item in GetDevices.list[i - 1].DeviceItems)
                    {
                        deviceName = item.Name + " | " + deviceName;
                    }
                    Console.WriteLine(i + ":" + GetDevices.list[i - 1].Name + " | " + deviceName);
                }

                Console.WriteLine("选择目标设备");

                int deviceNo = int.Parse(Console.ReadLine());

                Device device = GetDevices.list[deviceNo - 1];

                //HmiTarget hmiTarget = GetHmiTarget.GetHmiTargetFromDevice(device);
                
                PlcSoftware plcSoftware = GetPlcSoftware.GetPlcSoftwareFromDevic(device);

                foreach (PlcBlock block in plcSoftware.BlockGroup.Blocks)
                {
                    Console.WriteLine($"{block.Name}");
                }

                EnumerateAllBlock.EnumerateAllBlockGroupsAndSubgroups(plcSoftware);

                foreach (PlcBlock block in EnumerateAllBlock.plcBlocks)
                {
                    Console.WriteLine($"{block.Name}");
                }

                Console.WriteLine("输入查找字符");
                string findString = Console.ReadLine();
                Console.WriteLine("输入替换字符");
                string replaceString = Console.ReadLine();

                foreach (PlcBlock block in plcSoftware.BlockGroup.Blocks)
                {
                    var newName = block.Name.Replace(findString, replaceString);
                    block.Name = newName;
                }

                foreach (PlcBlock block in EnumerateAllBlock.plcBlocks)
                {
                    var newName = block.Name.Replace(findString, replaceString);
                    block.Name = newName;
                }

                _project.Save();
                _project.Close();

                Console.WriteLine("操作完成");
                
                Console.WriteLine("输入任意键离开");

                Console.ReadLine();

        
            }
        }
      

    }


}
